[#ftl]
[#include "commonDef.ftl" /]
${pack_own}

import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
${base_pack}.common.dto.ReqDTO;
${base_pack}.common.dto.ResDTO;
${base_pack}.common.dto.pagination.PageReqVO;
${base_pack}.common.exception.BizException;
${base_pack}.common.utils.CopyUtil;
${base_pack}.service.base.BaseManager;
${import_method_class}[#-- 方法对象引入 --]
${import_table_class}[#-- 表对象引入 --]
${import_class!''}
${pack_service}

/**
 * ${data_main.moduleKeyName}
 * ${'@author '+author_str!"admin"}
 * @date ${date_str}
 */
@Service("${lname_service}")
public class ${name_own} extends BaseManager implements ${name_service} {
[#if resource_table_class??]
${resource_table_class}[#-- 载入类 --]
[/#if]
[#if resource_class??]
${resource_class}[#-- 载入类 --]
[/#if]
    
[#if data_method??]
[#list data_method as mdx]
[#if mdx.needFlag == '1']
    /**
     * ${mdx.methodKeyName}
     */
    [#if (mdx.methodOprType!'') == 'getpage' ]
    @Override
    public ${mdx.name_res} get${ServiceMethodName(data_main,mdx)}(ReqDTO<${mdx.name_req}> reqInfo) throws Exception{
        ${mdx.name_req} reqData = reqInfo.getReqData();
        PageReqVO page = pageHandle(reqInfo.getPageInfo());//处理分页数据
        ${mdx.name_res} resDTO = BeanUtil.copyProperties(page, ${mdx.name_res}.class);
        [#assign eList = mdx.orderEntitys["req"]![]/]
        [#list eList as eVO]
        [#if (eVO.conditionSymbol!'')?length>0 && eVO.conditionSymbol=='lk' ]
        if(StringUtils.isNotBlank(${'reqData.get'+eVO.fieldJavaCode?cap_first+'()'})){
           reqData.set${eVO.fieldJavaCode?cap_first}("%"+${'reqData.get'+eVO.fieldJavaCode?cap_first+'()'}+"%");
        }
        [/#if]
        [/#list]
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData);
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        [#if data_table?? && data_table?size>1]
        [#assign eList = mdx.orderEntitys["res"]![]  tblx='null'  sqlx='null'/]
        [#list eList as eVO]
        [#if (eVO.useTableCode!'')?trim?length>0 && map_table[eVO.useTableCode]??]
        [#assign tblx=map_table[eVO.useTableCode]  sqlx=tblx.sqlInterfacesMap[eVO.useTableSqlCode] /][#break]
        [/#if]
        [/#list]
        [#if tblx=='null']
        [#assign tblx = data_table[0]  sqlx=tblx.sqlInterfacesMap['pageList'] /]
        [/#if]
        List<${tblx.name_entityVo}> list = ${tblx.lname_dao}.${sqlx.sqlId}(paramMap);
        resDTO.setData(BeanUtil.copyToList(list, ${tblx.name_resBase}.class));
        [#elseif data_table?? && data_table?size>0]
        [#assign tblx = data_table[0]  sqlx=tblx.sqlInterfacesMap['pageList'] /]
        List<${tblx.name_entityVo}> list = ${tblx.lname_dao}.${sqlx.sqlId}(paramMap);
        resDTO.setData(BeanUtil.copyToList(list, ${tblx.name_resBase}.class));
        [/#if]
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    [#elseif (mdx.methodOprType!'') == 'getdetail' ]
    @Override
    public ${mdx.name_res} get${ServiceMethodName(data_main,mdx)}(ReqDTO<${mdx.name_req}> reqInfo) throws Exception{
        ${mdx.name_req} reqData = reqInfo.getReqData();
        ${mdx.name_res} res = new ${mdx.name_res}();
        
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData);
        [#if data_table?? && data_table?size>1]
        [#assign eList = mdx.orderEntitys["res"]![] /]
        [#list eList as eVO]
        [#if (eVO.useTableCode!'')?trim?length>0 && eVO.fieldType == "1"][#-- 非对象和对象互斥，1和2,3不共存--]
        [#assign tblx = map_table[eVO.useTableCode]  sqlx=tblx.sqlInterfacesMap[eVO.useTableSqlCode] rdkey=tblx.tableJavaCode+'Record'/]
        ${tblx.name_entity} ${rdkey} = ${tblx.lname_dao}.${sqlx.sqlId}(paramMap);
        BeanUtil.copyProperties(${rdkey}, res);
        [/#if]
        [/#list]
        [#list eList as eVO]
        [#if (eVO.useTableCode!'')?trim?length>0 && eVO.fieldType == "2"][#-- 非对象和对象互斥，2,3共存--]
        [#assign tblx = map_table[eVO.useTableCode]  sqlx=tblx.sqlInterfacesMap[eVO.useTableSqlCode] rdkey=tblx.tableJavaCode+'Record'/]
        ${tblx.name_entity} ${rdkey} = ${tblx.lname_dao}.${sqlx.sqlId}(paramMap);
        res.set${eVO.fieldJavaCode?cap_first}(BeanUtil.copyProperties(${rdkey}, ${eVO.javaType!''}.class));
        [/#if]
        [/#list]
        [#list eList as eVO]
        [#if (eVO.useTableCode!'')?trim?length>0 && eVO.fieldType == "3"][#-- 非对象和对象互斥，2,3共存--]
        [#assign tblx = map_table[eVO.useTableCode]  sqlx=tblx.sqlInterfacesMap[eVO.useTableSqlCode]  rdkey=tblx.tableJavaCode+'Records' dtkey1=eVO.fieldJavaCode?uncap_first+'List'/]
        List<${tblx.name_entity}> ${rdkey} = ${tblx.lname_dao}.${sqlx.sqlId}(paramMap);
        if(${rdkey} !=null && ${rdkey}.size>0){
            List<${eVO.javaType!''}> ${dtkey1} = new ArrayList<>();
            for(${tblx.name_entity} x : ${rdkey}){
                ${dtkey1}.add(BeanUtil.copyProperties(x, ${eVO.javaType!''}.class));
            }
            res.set${eVO.fieldJavaCode?cap_first}(${dtkey1});
        }
        [/#if]
        [/#list]
        
        [#elseif data_table?? && data_table?size>0]
        [#assign tblx = data_table[0]  sqlx=tblx.sqlInterfacesMap['selectOne'] rdkey=tblx.tableJavaCode+'Record'/]
        ${tblx.name_entity} ${rdkey} = ${tblx.lname_dao}.${sqlx.sqlId}(paramMap);
        BeanUtil.copyProperties(${rdkey}, res);
        
        [/#if]
        return res;
    }
    [#elseif (mdx.methodOprType!'') == 'doadd' ]
    @Override
    public ${mdx.name_res} do${ServiceMethodName(data_main,mdx)}(ReqDTO<${mdx.name_req}> reqInfo) throws Exception{
        ${mdx.name_req} reqData = reqInfo.getReqData();
        ${mdx.name_res} res = new ${mdx.name_res}();
        Instant time = getSystemTime().getUtcTime();
        Map<String,Object> paramMap = new HashMap<>();
        
        [#if data_table?? && data_table?size>1]
        [#assign eList = mdx.orderEntitys["req"]![] /]
        [#list eList as eVO]
        [#if (eVO.useTableCode!'')?trim?length>0]
        [#assign tblx=map_table[eVO.useTableCode] sqlx=tblx.sqlInterfacesMap[eVO.useTableSqlCode] /]
        [#if tblx.primaryEntity?? && tblx.primaryEntity?size>0]
        [#if eVO.fieldType == "1" || eVO.fieldType == "2"]
        [#list tblx.primaryEntity as b]
        ${b.javaType} ${b.columnJavaCode} = null;
        [/#list]
        [#elseif eVO.fieldType == "3" ]
        [#list tblx.primaryEntity as b]
        Map<String,${b.javaType}> ${b.columnJavaCode}Maps = new HashMap<>();
        [/#list]
        [/#if]
        [/#if]
        [/#if]
        [/#list]
        
        [#list eList as eVO]
        [#if (eVO.useTableCode!'')?trim?length>0 && eVO.fieldType == "1"]
        [#assign tblx=map_table[eVO.useTableCode] sqlx=tblx.sqlInterfacesMap[eVO.useTableSqlCode] dtkey='reqData' rdkey=tblx.tableJavaCode?uncap_first+'Record' /]
        [@doaddForInsert mdx=mdx tblx=tblx sqlx=sqlx blankflag='' dtkey=dtkey rdkey=rdkey ftype='1'/]
        
        [/#if]
        [/#list]
        [#list eList as eVO]
        [#if (eVO.useTableCode!'')?trim?length>0 && eVO.fieldType == "2"]
        [#assign tblx=map_table[eVO.useTableCode] sqlx=tblx.sqlInterfacesMap[eVO.useTableSqlCode] dtkey='reqData.get'+eVO.fieldJavaCode?cap_first+'()' rdkey=tblx.tableJavaCode?uncap_first+'Record' /]
        [@doaddForInsert mdx=mdx tblx=tblx sqlx=sqlx blankflag='' dtkey=dtkey rdkey=rdkey ftype='1'/]
        
        [/#if]
        [/#list]
        [#list eList as eVO]
        [#if (eVO.useTableCode!'')?trim?length>0 && eVO.fieldType == "3" ]
        [#assign tblx=map_table[eVO.useTableCode] sqlx=tblx.sqlInterfacesMap[eVO.useTableSqlCode] dtkey1=eVO.fieldJavaCode +'List' dtkey=eVO.fieldJavaCode +'x' rdkey=tblx.tableJavaCode?uncap_first+'Record'/]
        List<${eVO.javaType!''}> ${dtkey1} = ${'reqData.get'+eVO.fieldJavaCode?cap_first+'()'};
        if(${dtkey1} != null && ${dtkey1}.size>0){
            for(${eVO.javaType!''} ${dtkey} : ${dtkey1}){
                [@doaddForInsert mdx=mdx tblx=tblx sqlx=sqlx blankflag='        ' dtkey=dtkey rdkey=rdkey ftype='2'/]
            }
        }
        
        [/#if]
        [/#list]
        [#assign eList = mdx.orderEntitys["res"]![] /]
        [#list eList as eVO]
        [#if (eVO.useTableCode!'')?trim?length>0]
        [#assign tblx=map_table[eVO.useTableCode] sqlx=tblx.sqlInterfacesMap[eVO.useTableSqlCode] dtkey='res' rdkey=tblx.tableJavaCode?uncap_first+'Record'/]
        [#if tblx.primaryEntity?? && tblx.primaryEntity?size>0]
        [@doeditForSelect tblx=tblx sqlx=sqlx dtkey=dtkey rdkey=rdkey /]
        [/#if]
        [/#if]
        [/#list]
        
        [#elseif data_table?? && data_table?size>0]
        [#assign tblx=data_table[0] sqlx=tblx.sqlInterfacesMap["insert"] dtkey='reqData' rdkey=tblx.tableJavaCode?uncap_first+'Record'/]
        [#list tblx.primaryEntity as b]
        ${b.javaType} ${b.columnJavaCode} = null;
        [/#list]
        [@doaddForInsert mdx=mdx tblx=tblx sqlx=sqlx blankflag='' dtkey=dtkey rdkey=rdkey ftype='1'/]
        
        [#assign tblx=data_table[0] sqlx=tblx.sqlInterfacesMap["selectOne"] dtkey='res' rdkey=tblx.tableJavaCode?uncap_first+'Record'/]
        [#if tblx.primaryEntity?? && tblx.primaryEntity?size>0]
        [@doeditForSelect tblx=tblx sqlx=sqlx dtkey=dtkey rdkey=rdkey /]
        [/#if]
        
        [/#if]
        return res;
    }
    [#elseif (mdx.methodOprType!'') == 'doedit' ]
    @Override
    public ${mdx.name_res} do${ServiceMethodName(data_main,mdx)}(ReqDTO<${mdx.name_req}> reqInfo) throws Exception{
        ${mdx.name_req} reqData = reqInfo.getReqData();
        ${mdx.name_res} res = new ${mdx.name_res}();
        Instant time = getSystemTime().getUtcTime();
        Map<String,Object> paramMap = new HashMap<>();
        
        [#if data_table?? && data_table?size>1]
        [#assign eList = mdx.orderEntitys["req"]![] /]
        [#list eList as eVO]
        [#if (eVO.useTableCode!'')?trim?length>0]
        [#assign tblx=map_table[eVO.useTableCode] sqlx=tblx.sqlInterfacesMap[eVO.useTableSqlCode] /]
        [#if tblx.primaryEntity?? && tblx.primaryEntity?size>0]
        [#if eVO.fieldType == "1" || eVO.fieldType == "2"]
        [#list tblx.primaryEntity as b]
        ${b.javaType} ${b.columnJavaCode} = null;
        [/#list]
        [#elseif eVO.fieldType == "3"]
        [#list tblx.primaryEntity as b]
        Map<String, ${b.javaType}> ${b.columnJavaCode}Maps = new HashMap<>();
        [/#list]
        [/#if]
        [/#if]
        [/#if]
        [/#list]
        
        [#list eList as eVO]
        [#if (eVO.useTableCode!'')?trim?length>0 && eVO.fieldType == "1"]
        [#assign tblx=map_table[eVO.useTableCode] sqlx=tblx.sqlInterfacesMap[eVO.useTableSqlCode] dtkey='reqData' rdkey=tblx.tableJavaCode?uncap_first+'Record'/]
        [@doeditForUpdate tblx=tblx  sqlx=sqlx  dtkey=dtkey  rdkey=rdkey ftype='1' blankflag=''/]
        [/#if]
        [/#list]
        [#list eList as eVO]
        [#if (eVO.useTableCode!'')?trim?length>0 && eVO.fieldType == "2"]
        [#assign tblx=map_table[eVO.useTableCode] sqlx=tblx.sqlInterfacesMap[eVO.useTableSqlCode] dtkey='reqData.get'+eVO.fieldJavaCode?cap_first+'()' rdkey=eVO.fieldJavaCode?uncap_first+'Record'/]
        [@doeditForUpdate tblx=tblx  sqlx=sqlx  dtkey=dtkey  rdkey=rdkey ftype='1' blankflag=''/]
        [/#if]
        [/#list]
        [#list eList as eVO]
        [#if (eVO.useTableCode!'')?trim?length>0 && eVO.fieldType == "3"]
        [#assign tblx=map_table[eVO.useTableCode] sqlx=tblx.sqlInterfacesMap[eVO.useTableSqlCode] dtkey1=eVO.fieldJavaCode?uncap_first+'List' dtkey=eVO.fieldJavaCode?uncap_first+'x' rdkey=eVO.fieldJavaCode?uncap_first+'Record'/]
        List<${eVO.javaType!''}> ${dtkey1} = reqData.get${eVO.fieldJavaCode?cap_first}();
        if(${dtkey1} !=null  && ${dtkey1}.size>0){
            for(${eVO.javaType!''} ${dtkey} : ${dtkey1}){
                [@doeditForUpdate tblx=tblx  sqlx=sqlx  dtkey=dtkey rdkey=rdkey ftype='2' blankflag='        '/]
            }
        }
        [/#if]
        [/#list]
        
        [#assign eList = mdx.orderEntitys["res"]![] /]
        [#list eList as eVO]
        [#if (eVO.useTableCode!'')?trim?length>0]
        [#assign tblx = map_table[eVO.useTableCode]  sqlx=tblx.sqlInterfacesMap[eVO.useTableSqlCode] dtkey='res' rdkey=tblx.tableJavaCode?uncap_first+'Record'/]
        [#if tblx.primaryEntity?? && tblx.primaryEntity?size>0]
        [@doeditForSelect tblx=tblx sqlx=sqlx dtkey=dtkey rdkey=rdkey /]
        [/#if]
        [/#if]
        [/#list]
        
        [#elseif data_table?? && data_table?size>0]
        [#assign tblx = data_table[0] sqlx=tblx.sqlInterfacesMap['updMap'] dtkey='reqData' rdkey=tblx.tableJavaCode?uncap_first+'Record'/]
        [#if tblx.primaryEntity?? && tblx.primaryEntity?size>0]
        [#list tblx.primaryEntity as b]
        ${b.javaType} ${b.columnJavaCode} = null;
        [/#list]
        [@doeditForUpdate tblx=tblx  sqlx=sqlx  dtkey=dtkey  rdkey=rdkey ftype='1' blankflag='' /]
        
        [#assign tblx=data_table[0] sqlx=tblx.sqlInterfacesMap['selectOne'] dtkey='res' rdkey=tblx.tableJavaCode?uncap_first+'Record' /]
        [@doeditForSelect tblx=tblx sqlx=sqlx dtkey=dtkey rdkey=rdkey /]
          
        [#else]
        [#assign sqlx2=tblx.sqlInterfacesMap['updMap'] /]
        
        [/#if]
        
        [/#if]
        return res;
    }
    [#elseif (mdx.methodOprType!'') == 'dodelete' ]
    @Override
    public ${mdx.name_res} do${ServiceMethodName(data_main,mdx)}(ReqDTO<${mdx.name_req}> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ${mdx.name_req} reqData = reqInfo.getReqData();
        ${mdx.name_res} res = new ${mdx.name_res}();
        Instant time = getSystemTime().getUtcTime();
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData);
        BeanUtil.copyProperties(reqData, res);
        [#if data_table?? && data_table?size>1]
        [#assign eList = mdx.orderEntitys["res"]![] /]
        [#list eList as eVO]
        [#if (eVO.useTableCode!'')?trim?length>0]
        [#assign tblx = map_table[eVO.useTableCode]  sqlx=tblx.sqlInterfacesMap[eVO.useTableSqlCode] rdkey=tblx.tableJavaCode+'Record' /]
        [#if tblx.primaryEntity?? && tblx.primaryEntity?size>0]
        ${tblx.name_entity} ${rdkey} = ${tblx.lname_dao}.${sqlx.sqlId}(paramMap);
        if(${rdkey} == null){
            return res;
        }
        [/#if]
        [/#if]
        [/#list]
        [#assign eList = mdx.orderEntitys["req"]![] /]
        [#list eList as eVO]
        [#if (eVO.useTableCode!'')?trim?length>0]
        [#assign tblx = map_table[eVO.useTableCode]  sqlx=tblx.sqlInterfacesMap[eVO.useTableSqlCode] /]
        ${tblx.lname_dao}.${sqlx.sqlId}(paramMap);
        [/#if]
        [/#list]
        
        [#elseif data_table?? && data_table?size>0]
        [#assign tblx=data_table[0] sqlx=tblx.sqlInterfacesMap['selectOne'] rdkey=tblx.tableJavaCode+'Record'/]
        [#if tblx.primaryEntity?? && tblx.primaryEntity?size>0]
        ${tblx.name_entity} ${rdkey} = ${tblx.lname_dao}.${sqlx.sqlId}(paramMap);
        BeanUtil.copyProperties(${rdkey}, res);
        [/#if]
        [#assign tblx=data_table[0] sqlx=tblx.sqlInterfacesMap['deleteMap'] /]
        ${tblx.lname_dao}.${sqlx.sqlId}(paramMap);
        
        [/#if]
        return res;
    }
    
    [#else]
    @Override
    public ${mdx.name_res} do${ServiceMethodName(data_main,mdx)}(ReqDTO<${mdx.name_req}> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ${mdx.name_req} reqData = reqInfo.getReqData();
        ${mdx.name_res} res = new ${mdx.name_res}();
        Instant time = getSystemTime().getUtcTime();
        
        return res;
    }
    [/#if]
[/#if]
[/#list]

[/#if]

[#-- 新增 --]
        [#macro doaddForInsert mdx tblx sqlx blankflag dtkey rdkey ftype]
        ${blankflag}${tblx.name_entity} ${rdkey} = new ${tblx.name_entity}();
        ${blankflag}BeanUtil.copyProperties(${dtkey}, ${rdkey});
        ${blankflag}initCreateInfo(${rdkey}, reqInfo, time);
        [#if tblx.primaryEntity?? && tblx.primaryEntity?size>0]
        [#list tblx.primaryEntity as b]
        [#assign idtype=''/]
        [#if (tblx.tableKeyType!'')?length==0]
        [#elseif (tblx.tableKeyImpl!'')?length>0 || tblx.tableKeyType == "3"]
        [#assign idtype=tblx.tableKeyImpl!''/]
        [#elseif b.javaType?upper_case == 'STRING' || tblx.tableKeyType == "1"]
        [#assign idtype='IdUtil.simpleUUID()'/]
        [#elseif b.javaType?upper_case != 'STRING' || tblx.tableKeyType == "2"]
        [#assign idtype='redisHashKey('+tblx.tableCode?replace("([a-z])([A-Z]+)","$1_$2","r")?upper_case+')'/] 
        [/#if]
        [#if ftype=="1"]
        ${blankflag}${b.columnJavaCode} = ${idtype};
        [#elseif ftype=="2"]
        ${blankflag}${b.javaType} ${b.columnJavaCode} = ${idtype};
        ${blankflag}${b.columnJavaCode+'Maps'}.put(${rdkey +'.get'+b.columnJavaCode?cap_first+'()'},${b.columnJavaCode});
        [/#if]
        ${blankflag}${rdkey+'.set'+b.columnJavaCode?cap_first}(${b.columnJavaCode});
        [/#list]
        [/#if]
        [#if table_rels?? && table_rels?size>0 && table_rels[tblx.tableJavaCode]?? && table_rels[tblx.tableJavaCode]?size>0]
        [#list table_rels[tblx.tableJavaCode] as rb]
        [#if rb.PTableType=="1"]
        ${blankflag}${rdkey+'.set'+rb.CColumnJavaCode?cap_first}(${rb.PColumnJavaCode});
        [#elseif rb.PTableType=="2"]
        ${blankflag}${rdkey+'.set'+rb.CColumnJavaCode?cap_first}(${rb.PColumnJavaCode+'Maps'}.get(${rdkey+'.get'+rb.CColumnJavaCode?cap_first+'()'}));
        [/#if]
        [/#list]
        [/#if]
        ${blankflag}${tblx.lname_dao}.${sqlx.sqlId}(${rdkey});
        [/#macro]
[#-- 更新类方法 --]
        [#macro doeditForUpdate tblx sqlx dtkey rdkey ftype blankflag]
        ${blankflag}${tblx.name_entity} ${rdkey} = new ${tblx.name_entity}();
        ${blankflag}BeanUtil.copyProperties(${dtkey}, ${rdkey});
        [#list tblx.primaryEntity as b]
        [#if ftype=="1"]
        ${blankflag}${b.columnJavaCode} = ${rdkey+'.get'+b.columnJavaCode?cap_first+'()'};
        [#elseif ftype=="2"]
        ${blankflag}${b.javaType} ${b.columnJavaCode} = ${rdkey+'.get'+b.columnJavaCode?cap_first+'()'};
        ${blankflag}${b.columnJavaCode+'Maps'}.put(${b.columnJavaCode});
        [/#if]
        [/#list]
        ${blankflag}initUpdateInfo(${rdkey}, reqInfo, time);
        [#list tblx.primaryEntity as b]
        paramMap.put("${b.columnJavaCode}", ${b.columnJavaCode});
        [/#list]
        ${blankflag}int ${'updNum'+tblx.tableJavaCode?cap_first} = ${tblx.lname_dao}.${sqlx.sqlId}(${rdkey},paramMap);
        [/#macro]
[#-- 根据主键查询主表单条基础记录 --]
        [#macro doeditForSelect tblx sqlx dtkey rdkey]
        paramMap.clear();
        [#list tblx.primaryEntity as b]
        paramMap.put("${b.columnJavaCode}", ${b.columnJavaCode});
        [/#list]
        ${rdkey} = ${tblx.lname_dao}.${sqlx.sqlId}(paramMap);
        BeanUtil.copyProperties(${rdkey}, ${dtkey});
        [/#macro]
        
}